import { ITimer } from "../../api/timer/ITimer";
import { E_MailId } from "../defs/mail/E_MailId";
import { E_MailRewardStatus } from "../defs/mail/E_MailRewardStatus";
import { Mail } from "../struct/Mail";
import { Player } from "../struct/Player";
import { SDBag } from "./SDBag";
// 原导入路径有误，推测 E_MailId 定义在某个具体文件中，这里假设为 E_MailId.ts

/**预创建 */
const PRE_GENERAL_REWARD_CONFIG = { preGenerate: true };


/**
 * 数据-邮箱
 */
export abstract class SDMailBox {


    /**
     * 获取更新倒计时
     * @param player 
     * @param now 
     * @returns 
     */
    public static getUpdateCountDown(player: Player, now = ITimer.getServerTime()): number {
        const vs = player.mailBox.getMailValues();
        let delay = 0;
        for (let i = vs.length - 1; i >= 0; i--) {
            const mail = vs[i];
            if (mail.validTime > 0) {
                const d = mail.validTime - now;
                if (d <= 0) {
                    return -1;
                }
                if (d < delay) {
                    delay = d;
                }
            }
        }
        return delay;
    }


    /**
     * 是否有装备可以放到背包中
     * @param player 
     * @returns 
     */
    public static isHaveEquipCanMoveToBag(player: Player, now = ITimer.getServerTime()): boolean {
        if (!SDBag.isBagFull(player)) {
            const vs = player.mailBox.getMailValues();
            for (let i = vs.length - 1; i >= 0; i--) {
                const mail = vs[i];
                if (mail.rewardStatus !== E_MailRewardStatus.Doing) {
                    continue;
                }
                if (mail.validTime > 0 && mail.validTime < now) {
                    continue;
                }
                const dc = mail.drop;
                if (dc.getEquipKeys().length > 0) {
                    return true;
                }
            }
        }

        return false;
    }


    /**
     * 只包含装备
     * @param mail 
     * @returns 
     */
    public static isOnlyContainsEquip(mail: Mail): boolean {

        if (!mail || !mail.drop) {
            return false;
        }

        const drop = mail.drop;
        if (drop.getItemKeys().length > 0
            || drop.getPetKeys().length > 0) {
            return false;
        }

        return true;
    }



}
